package cn.yanxiaohui.common.exception;

import sun.misc.Queue;

/**
 * 异常错误编码
 * 参考ICU的错误代码定义
 * ErrorCode > 0，为异常代码
 * ErrorCode = 0，无错误
 * ErrorCode < 0，为Warning
 *
 */
public class APIExceptionErrorCode {

    /**
     * Start of information results (semantically successful)
     */
    public static final int U_ERROR_INFO_START = -128;
    /**
     * A resource bundle lookup returned a fallback result (not an error)
     */
    public static final int U_USING_FALLBACK_ERROR = -128;
    /**
     * A resource bundle lookup returned a result from the root locale (not an
     * error)
     */
    public static final int U_USING_DEFAULT_ERROR = -127;
    /**
     * A SafeClone operation required allocating memory (informational
     * only
     */
    public static final int U_SAFECLONE_ALLOCATED_ERROR = -126;
    /**
     * This must always be the last warning value to indicate the limit for
     * UErrorCode warnings (last warning code +1)
     */
    public static final int U_ERROR_INFO_LIMIT = -125;

    /**
     * No error, no warning
     */
    public static final int U_ZERO_ERROR = 0;

    /**
     * 非法参数
     */
    public static final int U_ILLEGAL_ARGUMENT_ERROR = 1;
    /**
     * 请求的资源不存在
     */
    public static final int U_MISSING_RESOURCE_ERROR = 2;

    /**
     * 违反唯一性约束
     */
    public static final int U_UNIQUE_CONSTRAINT_ERROR = 3;
    /**
     * 无效格式
     */
    public static final int U_INVALID_FORMAT_ERROR = 4;
    /**
     * 数据库访问异常
     */
    public static final int U_DATA_ACCESS_ERROR = 5;
    /**
     * 文件访问异常
     */
    public static final int U_FILE_ACCESS_ERROR = 6;
    /**
     * 系统异常
     */
    public static final int U_INTERNAL_PROGRAM_ERROR = 7;
    /**
     * 内存分配异常
     */
    public static final int U_MEMORY_ALLOCATION_ERROR = 8;
    /**
     * 数组越界
     */
    public static final int U_INDEX_OUTOFBOUNDS_ERROR = 9;
    /**
     * 解析类异常
     */
    public static final int U_PARSE_ERROR = 10;
    /**
     * 非法字符
     */
    public static final int U_ILLEGAL_CHAR_FOUND = 11;
    /**
     * 缓冲区溢出
     */
    public static final int U_BUFFER_OVERFLOW_ERROR = 12;
    /**
     * 程序不支持
     */
    public static final int U_UNSUPPORTED_ERROR = 13;
    /**
     * 可用空间不足
     */
    public static final int U_NO_SPACE_AVAILABLE = 14;
    /**
     * 数据关联性不完整
     */
    public static final int U_VIOLATE_ASSOCIATION = 15;
    /**
     *
     */
    public static final int U_NOT_PROPERLY_CONFIGURE = 16;
    /**
     * 远程服务错误
     */
    public static final int U_REMOTE_ERROR = 18;
    
    /**
     * This must always be the last value to indicate the limit for UErrorCode
     * (last error code +1)
     */
    public static final int U_ERROR_LIMIT = 17;
    

    public static String getErrorName(int ec) {
        return ERROR_NAME_MAP[ec];
    }

    public static boolean isSuccess(int ec) {
        return (ec <= U_ZERO_ERROR);
    }

    public static boolean isFailure(int ec) {
        return (ec > U_ZERO_ERROR);
    }
    
    /**
     * Array of error code names corresponding to the errorcodes.
     * ie ERROR_NAME_MAP[0] = name of U_ZERO_ERROR
     */
    private static final String ERROR_NAME_MAP[] = {
            "U_ZERO_ERROR",
            "U_MISSING_RESOURCE_ERROR",
            "U_ILLEGAL_ARGUMENT_ERROR",
            "U_UNIQUE_CONSTRAINT_ERROR",
            "U_INVALID_FORMAT_ERROR",
            "U_DATA_ACCESS_ERROR",
            "U_FILE_ACCESS_ERROR",
            "U_INTERNAL_PROGRAM_ERROR",
            "U_MEMORY_ALLOCATION_ERROR",
            "U_INDEX_OUTOFBOUNDS_ERROR",
            "U_PARSE_ERROR",
            "U_ILLEGAL_CHAR_FOUND",
            "U_BUFFER_OVERFLOW_ERROR",
            "U_UNSUPPORTED_ERROR",
            "U_NO_SPACE_AVAILABLE",
            "U_VIOLATE_ASSOCIATION",
            "U_NOT_PROPERLY_CONFIGURE",
            "U_REMOTE_ERROR"
    };
    
    /**
     * 系统参数异常
     */
    public enum sysError {
    	APP_KEY_ERROR(10000,"APP_KEY错误，可能为非法请求"),
    	SYS_PARAM_ERROR(10001,"系统参数不正确"),
    	TIME_EXPIRE_ERROR(10002,"系统时间差超过预设值，可能为非法请求"),
    	ACCESS_TOKEN_ERROR(10003,"ACCESS_TOKEN错误，可能为非法请求"),
    	ACCESS_TOKEN_EXPIRE(10004,"ACCESS_TOKEN已过期"),
    	SIGN_ERROR(10005,"签名验证不通过"),
    	NOT_EXIST(10006,"为必填项"),
    	TRANSFORM_ERROR(10007,"类型转换错误"),
    	SERVICE_ERROR(10008,"服务异常,请求不可达"),
    	FILE_NOT_FOUND(10009, "找不到指定文件"),
    	FILE_IO_EXCEPTION(U_FILE_ACCESS_ERROR, "文件访问异常"),
    	REQUEST_NOT_EXIST_FILE(10010, "请求中未捕获上传文件");

        private int code;
        private String message;

        private sysError(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
    
    /**
     * 业务异常
     */
    public enum BizError {
    	SERVICE_NOT_FOUND(39999,"服务不存在"),
    	RESULT_IS_NULL(40000,"查询结果为空"),
    	USER_SAME(40001,"用户名已存在"),
    	PASSWORD_IS_NULL(40001,"密码不能为空"),


        /***
         * 其它业务异常已9开头
         */
        QUEUE_NAME_OR_EXCHANGE_NAME_NULL(90001, "队列名称或交换机名称不能为空"),
        IO_EXCEPTION(90002, "文件流错误"),
        CODEC_FACTORY_NULL(90003, "序列化工厂不存在"),
        MESSAGE_ERROR(90004, "发送消息出现异常");

        private int code;
        private String message;

        private BizError(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}